package com.itheima.java.ai.langchin4j.controller;

import com.itheima.java.ai.langchin4j.entity.HealthRecord;
import com.itheima.java.ai.langchin4j.server.HealthRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "健康档案管理")
@RestController
@RequestMapping("/api/health")
@CrossOrigin(origins = "*")
public class HealthRecordController {
    
    @Autowired
    private HealthRecordService healthRecordService;
    
    // 默认用户ID - 权限控制已移除
    private static final Long DEFAULT_USER_ID = 1L;
    
    @Operation(summary = "获取健康档案")
    @GetMapping("/record")
    public ResponseEntity<Map<String, Object>> getHealthRecord(HttpServletRequest request) {
        // 使用默认用户ID - 权限控制已移除
        Long userId = DEFAULT_USER_ID;
        System.out.println("HealthRecordController.getHealthRecord - userId: " + userId);
        Map<String, Object> response = new HashMap<>();
        
        try {
            HealthRecord healthRecord = healthRecordService.getByUserId(userId);
            response.put("success", true);
            response.put("data", healthRecord);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    @Operation(summary = "更新健康档案")
    @PutMapping("/record")
    public ResponseEntity<Map<String, Object>> updateHealthRecord(@RequestBody HealthRecord healthRecord, HttpServletRequest request) {
        // 使用默认用户ID - 权限控制已移除
        Long userId = DEFAULT_USER_ID;
        healthRecord.setUserId(userId);
        Map<String, Object> response = new HashMap<>();
        
        try {
            boolean success = healthRecordService.saveOrUpdateHealthRecord(healthRecord);
            if (success) {
                response.put("success", true);
                response.put("message", "更新成功");
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "更新失败");
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    @Operation(summary = "初始化健康档案")
    @PostMapping("/record/init")
    public ResponseEntity<Map<String, Object>> initHealthRecord(@RequestBody Map<String, Object> requestBody) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 调试：打印请求体内容
            System.out.println("Request body: " + requestBody);
            
            // 从请求体中获取用户ID
            Object userIdObj = requestBody.get("userId");
            System.out.println("UserId object: " + userIdObj);
            
            if (userIdObj == null) {
                response.put("success", false);
                response.put("message", "用户ID不能为空，请求体: " + requestBody);
                return ResponseEntity.badRequest().body(response);
            }
            
            Long userId = Long.valueOf(userIdObj.toString());
            System.out.println("Parsed userId: " + userId);
            
            HealthRecord healthRecord = healthRecordService.initHealthRecord(userId);
            response.put("success", true);
            response.put("message", "初始化成功");
            response.put("data", healthRecord);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
}